package com.ryl.yomall.user.controller;

import com.ryl.yomall.model.common.constant.RedisConst;
import com.ryl.yomall.model.common.result.Result;
import com.ryl.yomall.model.common.util.AuthContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @Date 21:10
 * @Author ryl
 */
/**
 * @Date 21:10
 * @Author ryl
 */
@RestController
@RequestMapping("/api/user/passport")
public class LogoutApiController {
    @Autowired
    private RedisTemplate redisTemplate;
    @GetMapping("/logout")
    public Result logout(HttpServletRequest request){

//        Cookie[] cookies = request.getCookies ( );
//
//        for (Cookie cookie : cookies) {
//            if("token".equals ( cookie.getName ()) ){
//                String token= cookie.getValue ( );
//                redisTemplate.delete ( RedisConst.USER_LOGIN_KEY_PREFIX+token );
//            }
//        }
        String token = request.getHeader ( "token" );
        Cookie[] cookies = request.getCookies ( );
        redisTemplate.delete ( RedisConst.USER_LOGIN_KEY_PREFIX+token );
        redisTemplate.delete ( "originUrl" );
        return Result.ok ();
    }
}